package caculator;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.event.*;
import javafx.stage.Stage;
import javafx.geometry.*;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.KeyCombination;

public class CaculatorScienceType extends Application  implements EventHandler<ActionEvent>{
	BorderPane p0=new BorderPane();
	BorderPane p1=new BorderPane();
	BorderPane p2=new BorderPane();
	GridPane p3=new GridPane();
	GridPane p4=new GridPane();
	HBox p5 = new HBox();
	BorderPane p6=new BorderPane();
	BorderPane p7=new BorderPane();
	HBox p8=new HBox();
	GridPane p9=new GridPane();
	VBox p10=new VBox();
	VBox p11=new VBox();
	HBox p13=new HBox();
	BorderPane p12=new BorderPane();
	TextField tf = new TextField("0");
	
	Button[][] button0={{new Button(""),new Button("lnv"),new Button("ln"),new Button("("),new Button(")")},
				{new Button("Int"), new Button("sinh"),new Button("sin"),new Button("x²"),new Button("n!")},
				{new Button("arccos"),new Button("cosh"),new Button("cos"), new Button("xⁿ"),new Button("ⁿ√x")},
				{new Button("π"),new Button("tanh"),new Button("tan"), new Button("x³"), new Button("³√x")},
				{new Button("F-E"),new Button("eⁿ"),new Button("2*π"), new Button("log"),new Button("10ⁿ")}};
	Button[][] button1={{new Button("MC"), new Button("MR"),new Button("MS"), new Button("M+"), new Button("M-")},
			    {new Button("←"),new Button("CE"),new Button("C"), new Button("±"), new Button("√")},
			    {new Button("7"),new Button("8"),new Button("9"),new Button("/"), new Button("%")},
			    {new Button("4"),new Button("5"),new Button("6"),new Button("*"),new Button("1/x")}};
	Button[][] button2={{new Button("3"),new Button("-")},
			    {new Button("."),new Button("+")}};
	Button Zero = new Button("0");
	Button One = new Button("1");
	Button Two = new Button("2");
	Button Equals = new Button("=");
	RadioButton r0 =new RadioButton("梯度");	
	RadioButton r1 =new RadioButton("弧度");
	RadioButton r2 =new RadioButton("度");
	Menu menuFile = new Menu("查看");
    Menu menuEdit = new Menu("编辑");
    Menu menuView = new Menu("帮助");
    MenuItem add0 = new MenuItem("科学型");
    MenuItem add1 = new MenuItem("标准型");
    MenuItem add2 = new MenuItem("复制");
    MenuItem add3 = new MenuItem("粘贴");
    MenuItem add4 = new MenuItem("关于计算器");
		
	@Override
	public void start(Stage primaryStage) {		
				
		p0.setTop(p12);
		p0.setLeft(p1);
		p0.setRight(p2);
		p1.setTop(p5);
		p1.setBottom(p3);
		p2.setTop(p4);
		p2.setBottom(p7);
		p7.setLeft(p6);
		p7.setCenter(p9);
		p7.setRight(p10);
		p6.setTop(p8);
		p6.setCenter(p11);
		p12.setCenter(tf);
		p12.setTop(p13);
		
		tf.setPrefSize(40,60);
		tf.setFont(Font.font(20));
		Zero.setPrefSize(130, 40);
		One.setPrefSize(60,40);
		Two.setPrefSize(60, 40);
		Equals.setPrefSize(60, 90);		
		for(int i=0;i<5;i++){
			for(int j=0;j<5;j++){
			button0[j][i].setPrefSize(60,40);
			p3.add(button0[j][i],i,j);
			button0[i][j].setOnAction(this);
			
			}
		}
		for(int i=0;i<5;i++){
			for(int j=0;j<4;j++){
			button1[j][i].setPrefSize(60,40);
			p4.add(button1[j][i],i,j);
			button1[j][i].setOnAction(this);
			}
		}	
		for(int i=0;i<2;i++){
			for(int j=0;j<2;j++){
			button2[i][j].setPrefSize(60,40);
			p9.add(button2[i][j],i,j);
			button2[i][j].setOnAction(this);
			}
		}
		Zero.setOnAction(this);
		One.setOnAction(this);
		Two.setOnAction(this);
		Equals.setOnAction(this);
			
		ToggleGroup g =new ToggleGroup();
		r0.setToggleGroup(g);
		r1.setToggleGroup(g);
		r2.setToggleGroup(g);
		r0.setSelected(true);
		r1.setOnAction(this);
		r2.setOnAction(this);				

		tf.setAlignment(Pos.BOTTOM_RIGHT);
	    p0.setPadding(new Insets(10,10,10,10));
		p5.setAlignment(Pos.CENTER);
		p5.setSpacing(60);
		p5.getChildren().addAll(r0,r1,r2);
		p3.setAlignment(Pos.CENTER);
		p3.setVgap(8);
		p3.setHgap(8);
		p4.setAlignment(Pos.CENTER);
		p4.setVgap(8);
		p4.setHgap(8);
		p9.setAlignment(Pos.CENTER);
		p9.setVgap(8);
		p9.setHgap(8);
		p11.setAlignment(Pos.CENTER);
		p11.setPadding(new Insets(8,0,0,0));
		p11.getChildren().add(Zero);
		p8.setAlignment(Pos.CENTER);
		p8.setSpacing(8);
		p8.getChildren().addAll(One,Two);
		p10.setAlignment(Pos.CENTER);
		p10.getChildren().add(Equals);
		
		MenuBar menuBar = new MenuBar();       
        menuFile.getItems().addAll(add0,add1);   
        menuEdit.getItems().addAll(add2,add3);   
        menuView.getItems().addAll(add4); 
        menuBar.getMenus().addAll(menuFile, menuEdit, menuView); 
        p13.getChildren().addAll(menuBar);  
        add1.setOnAction(e->{		        
			CaculatorStandardType caculator=new CaculatorStandardType();
			Stage stage=new Stage();
			caculator.start(stage);
			primaryStage.close();});
        add2.setOnAction(this);
        add3.setOnAction(this);
        add4.setOnAction(this);
        add0.setAccelerator(
      		     KeyCombination.keyCombination("Ctrl+1"));
      	add1.setAccelerator(
      		     KeyCombination.keyCombination("Ctrl+2"));
        add2.setAccelerator(
      	         KeyCombination.keyCombination("Ctrl+c"));
        add3.setAccelerator(
      			 KeyCombination.keyCombination("Ctrl+v"));
        
		Scene scene = new Scene(p0);
		primaryStage.setTitle("计算器");
		primaryStage.setScene(scene);
		primaryStage.show();
		primaryStage.setResizable(false);	
		
	}


public static void main(String[] args) {
	launch(args);
}

int x;
double num1,num2;
@Override
public void handle(ActionEvent e){					
			if(e.getSource()==r2){
				tf.setText(String.valueOf(Math.toRadians(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==r1){
				tf.setText(String.valueOf(Math.toDegrees(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[0][2]){
				if((int)Math.log(Double.parseDouble(tf.getText()))==Math.log(Double.parseDouble(tf.getText())))
				tf.setText(String.valueOf((int)Math.log(Double.parseDouble(tf.getText()))));
				else tf.setText(String.valueOf(Math.log(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[0][3]){
				 tf.setText(tf.getText()+"(");
			}
			else if(e.getSource()==button0[0][4]){
				 tf.setText(tf.getText()+")");
			}
			else if(e.getSource()==button0[1][0]){
				tf.setText(String.valueOf((int)Double.parseDouble(tf.getText())));
			}
			else if(e.getSource()==button0[1][1]){
				if((int)Math.sinh(Double.parseDouble(tf.getText()))==Math.sinh(Double.parseDouble(tf.getText())))
					tf.setText(String.valueOf((int)Math.sinh(Double.parseDouble(tf.getText()))));
					else tf.setText(String.valueOf(Math.sinh(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[1][2]){
				if((int)Math.sin(Double.parseDouble(tf.getText()))==Math.sin(Double.parseDouble(tf.getText())))
					tf.setText(String.valueOf((int)Math.sin(Double.parseDouble(tf.getText()))));
					else tf.setText(String.valueOf(Math.sin(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[1][3]){
				if((int)Math.pow(Double.parseDouble(tf.getText()), 2)==Math.pow(Double.parseDouble(tf.getText()), 2))
					tf.setText(String.valueOf((int)Math.pow(Double.parseDouble(tf.getText()), 2)));
					else tf.setText(String.valueOf(Math.pow(Double.parseDouble(tf.getText()), 2)));
			}
			else if(e.getSource()==button0[1][4]){
				double i,a=Double.parseDouble(tf.getText());
				
				for(i=1;a>1;a--)
					i=i*a;
				if((int)i==i)
				    tf.setText(String.valueOf((int)i));
				else tf.setText(String.valueOf(i));
			}
			else if(e.getSource()==button0[2][0]){
				tf.setText(String.valueOf(Math.acos(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[2][1]){
				if((int)Math.cosh(Double.parseDouble(tf.getText()))==Math.cosh(Double.parseDouble(tf.getText())))
					tf.setText(String.valueOf((int)Math.cosh(Double.parseDouble(tf.getText()))));
					else tf.setText(String.valueOf(Math.cosh(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[2][2]){
				if((int)Math.cos(Double.parseDouble(tf.getText()))==Math.cos(Double.parseDouble(tf.getText())))
					tf.setText(String.valueOf((int)Math.cos(Double.parseDouble(tf.getText()))));
					else tf.setText(String.valueOf(Math.cos(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[2][3]){
				num1=Double.parseDouble(tf.getText());  
	            tf.setText("");  
	            x=4;
			}
			else if(e.getSource()==button0[2][4]){
				num1=Double.parseDouble(tf.getText());  
	            tf.setText("");  
	            x=5;
			}

			else if(e.getSource()==button0[3][0]){
				tf.setText(String.valueOf(Math.PI));
			}
			else if(e.getSource()==button0[3][1]){
				if((int)Math.tanh(Double.parseDouble(tf.getText()))==Math.tanh(Double.parseDouble(tf.getText())))
					tf.setText(String.valueOf((int)Math.tanh(Double.parseDouble(tf.getText()))));
					else tf.setText(String.valueOf(Math.tanh(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[3][2]){
				if((int)Math.tan(Double.parseDouble(tf.getText()))==Math.tan(Double.parseDouble(tf.getText())))
					tf.setText(String.valueOf((int)Math.tan(Double.parseDouble(tf.getText()))));
					else tf.setText(String.valueOf(Math.tan(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[3][3]){
				if((int)Math.pow(Double.parseDouble(tf.getText()), 3)==Math.pow(Double.parseDouble(tf.getText()), 3))
					tf.setText(String.valueOf((int)Math.pow(Double.parseDouble(tf.getText()), 3)));
					else tf.setText(String.valueOf(Math.pow(Double.parseDouble(tf.getText()), 3)));
			}
			else if(e.getSource()==button0[3][4]){
				if((int)Math.pow(Double.parseDouble(tf.getText()), 1.0/3)==Math.pow(Double.parseDouble(tf.getText()), 1.0/3))
					tf.setText(String.valueOf((int)Math.pow(Double.parseDouble(tf.getText()), 1.0/3)));
					else tf.setText(String.valueOf(Math.pow(Double.parseDouble(tf.getText()), 1.0/3)));
			}
			else if(e.getSource()==button0[4][0]){
				double b=Double.parseDouble(tf.getText());
				if(Math.abs(Double.parseDouble(tf.getText()))<10)
					tf.setText(String.valueOf(b+"e+0"));
				else{
					int i=0;
					double a=Double.parseDouble(tf.getText());
				while(Math.abs(a)>=10){
					a=a/10;
					i++;
				}
				tf.setText(String.valueOf(b/Math.pow(10, i)+"e+"+i));
				}
			}
			else if(e.getSource()==button0[4][1]){
				tf.setText(String.valueOf(Math.pow(Math.E, Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[4][2]){
				tf.setText(String.valueOf(Math.PI*2));
			}
			else if(e.getSource()==button0[4][3]){
				if((int)Math.log10(Double.parseDouble(tf.getText()))==Math.log10(Double.parseDouble(tf.getText())))
					tf.setText(String.valueOf((int)Math.log10(Double.parseDouble(tf.getText()))));
					else tf.setText(String.valueOf(Math.log10(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==button0[4][4]){
				tf.setText(String.valueOf(Math.pow(10, Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()==Zero){
				tf.setText(tf.getText()+"0");
			}
			else if(e.getSource()==One){
				tf.setText(tf.getText()+"1");
			}
			else if(e.getSource()==Two){
				tf.setText(tf.getText()+"2");			
			}
			else if(e.getSource()==button2[0][0]){
				tf.setText(tf.getText()+"3");
			}
			else if(e.getSource()==button1[3][0]){
				tf.setText(tf.getText()+"4");
			}
			else if(e.getSource()==button1[3][1]){
				tf.setText(tf.getText()+"5");
			}
			else if(e.getSource()==button1[3][2]){
				tf.setText(tf.getText()+"6");
			}
			else if(e.getSource()==button1[2][0]){
				tf.setText(tf.getText()+"7");
			}
			else if(e.getSource()==button1[2][1]){
				tf.setText(tf.getText()+"8");
			}
			else if(e.getSource()==button1[2][2]){
				tf.setText(tf.getText()+"9");
			}			
			else if(e.getSource()==button2[1][0]){
				tf.setText(tf.getText()+".");
			}
			else if(e.getSource()==button1[1][3]){
				tf.setText("-"+tf.getText());
			}
			else if(e.getSource()==button1[1][4]){
				tf.setText(String.valueOf(Math.pow(Double.parseDouble(tf.getText()),0.5)));
			}
			else if(e.getSource()==button1[3][4]){
				tf.setText(String.valueOf(1/(Double.parseDouble(tf.getText()))));
			}
			else if(e.getSource()== button1[1][1]){
				tf.setText("0");			      
			}
			else if(e.getSource()==button1[1][2]){
				tf.setText("0");
			}
			else if(e.getSource()==button1[1][0]){	
					String text = new String(tf.getText());
					int j = text.length();
					if(j>0)
					{
						text = text.substring(0,j - 1);
						if(text.length() == 0)
							tf.setText("0");
						else
							tf.setText(text);
					}
		}			
			else if(e.getSource()==add2){		        
		            tf.selectAll();
		            tf.copy();		        
		    }
			else if(e.getSource()==add3){
				tf.selectAll();
				tf.paste();
		    }
			else if(e.getSource()==add4){			
				Alert alert=new Alert(AlertType.INFORMATION);
				alert.setTitle("关于计算器");
				alert.setHeaderText("这是一个由陈美蓉、叶华丽、曾琬婷组队合作编写的计算器代码 !\n"
						+ "功能实现说明：\n1.Inv,弧度，MS,MC,MR,M+,M-这几个按钮的功能没有实现；\n"
						+ "2.括号运算没有实现；\n3.输入不是数字等异常的功能没有实现；\n除以上说明之外的所有按钮功能均实现。\n"
						+ "某些按键使用说明：\n1.弧度与度按钮的操作：先输入一个数，然后点击要转换成的单位按钮，再点击数学函数按钮。\n"
						+ "例如：输入90，点击“度”按钮，然后点击“sin”按钮，结果为1。\n2.“xⁿ”“ⁿ√x”这两个按钮的操作：\n"
						+ "第一个输入的数字为x，第二次输入的数字为n。\n例如：先输入16，然后点击ⁿ√x按钮，再输入4，结果为2。\n"
						+ "3.“%”按钮的操作，先输入一个数，然后点击该按钮，再输入第二个数，表示第一个数对第二个数取模。\n"
						+ "例如：输入2，点击%按钮，再输入3，结果为2。\n注：此计算器代码从头到尾都是我们自己实现的，没有半点抄袭！！");
				alert.setContentText("");
				alert.show(); 			    		
    		   } 
			
			else if(e.getSource()==button1[2][4])  
			        {  		              
			            num1=Double.parseDouble(tf.getText());  
			            tf.setText("");  
			            x=6;  
			        }  
			  else if(e.getSource()==button2[1][1])  
		        {  
		              
		            num1=Double.parseDouble(tf.getText());  
		            tf.setText("");  
		            x=0;  
		        }  
		        else if(e.getSource()==button2[0][1])  
		        {  
		        	num1=Double.parseDouble(tf.getText());
		            tf.setText("");  
		            x=1;  
		        }  
		        else if(e.getSource()==button1[3][3])  
		         {  
		                num1=Double.parseDouble(tf.getText());
		                tf.setText("");  
		                x=2;  
		          }  
		         else if(e.getSource()==button1[2][3])  
		            {  
		                num1=Double.parseDouble(tf.getText());  
		                tf.setText("");  
		                x=3;  
		            }  
		         else if(e.getSource()==Equals)  
		         {  
		              
		        	 num2=Double.parseDouble(tf.getText());   
		             switch(x)  
		             {  
		             case  0 : tf.setText("" + (num1 + num2));break;  
		             case  1 : tf.setText("" + (num1 - num2));break;  
		             case  2 : tf.setText("" + (num1 * num2));break;  
		             case  3 :  if(num2!=0){tf.setText("" + (num1 / num2));break;}else tf.setText("除数不能为0");break;
		             case  4 : tf.setText("" + (String.valueOf(Math.pow(num1,num2))));break;  
		             case  5 : tf.setText("" + (String.valueOf(Math.pow(num1,1/num2))));break;  
		             case  6 : tf.setText("" + (String.valueOf(((int)num1%(int)num2))));break;
		             }  
		              
		         }  
			}
}				
			



